package jblip.apps.clip.cmds;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import jblip.BlipClient;

public class PostPicture extends ClipCommand {

  @Override
  public void execute(BlipClient client, String[] args) {
    final File pic = new File(args[0]);
    if (!pic.isFile() || !pic.canRead()) {
      System.out.println("ERROR: File can't be read");
    }

    final StringBuilder msg = new StringBuilder(160);

    msg.append(args[1]);
    for (int i = 2; i < args.length; i++) {
      msg.append(' ');
      msg.append(args[i]);
    }

    try {
      client.postMessage(msg.toString(), new FileInputStream(pic));
    } catch (FileNotFoundException e) {
    }

    if (client.getLastError() == null) {
      System.out.println("Message posted.");
    } else {
      System.out.println("ERROR: " + client.getLastError().toString());
    }

  }

  @Override
  public String getName() {
    return "postpic";
  }

  @Override
  public String getUsageMessage() {
    return "<filename> <message>";
  }

  @Override
  public int getRequiredArgsCount() {
    return 2;
  }

  @Override
  public int getMaximumArgsCount() {
    return Integer.MAX_VALUE;
  }

}
